import 'dart:convert';

import 'package:floor/floor.dart';
import 'package:json_annotation/json_annotation.dart';

part 'message_db.g.dart';

/// 消息实体类
/// 用于存储聊天消息的详细信息，包括会话信息、消息内容、发送者信息等
/// 该类同时支持数据库存储和JSON序列化
@Entity(tableName: 'messages')
@JsonSerializable()
class MessageDb {
  /// 会话ID
  @JsonKey(name: "conv_id")
  String? convId;

  /// 会话类型
  /// 用于区分单聊、群聊等不同类型的会话
  @JsonKey(name: "conv_type")
  int? convType;

  /// 是否是自己发送的消息
  @JsonKey(name: "is_myself")
  bool? isMyself;

  /// 消息客户端ID
  /// 用作消息的唯一标识，由客户端生成
  @JsonKey(name: "msg_client_id")
  String? msgClientId;

  /// 消息内容
  @JsonKey(name: "msg_status_tips")
  String? msgStatusTips;

  /// 消息内容
  @JsonKey(name: "msg_content")
  String? msgContent;

  /// 消息内容格式
  /// 1: JSON格式
  /// 2: Protobuf格式
  @JsonKey(name: "msg_content_fmt")
  int? msgContentFmt;

  /// 消息内容类型
  /// 可选值：text, image, audio, video, file等
  /// 支持扩展新的类型
  @JsonKey(name: "msg_content_type")
  String? msgContentType;

  /// 服务端消息ID
  // @PrimaryKey(autoGenerate: true)
  @PrimaryKey()
  @JsonKey(name: "msg_id")
  String? msgId;

  /// 消息已读时间
  @JsonKey(name: "msg_read_time")
  String? msgReadTime;

  /// 消息状态
  /// 1: 发送中
  /// 2: 已发送
  /// 3: 已送达
  /// 4: 已读
  @JsonKey(name: "msg_status")
  int? msgStatus;

  /// 消息类型
  @JsonKey(name: "msg_type")
  String? msgType;

  /// 消息发送时间
  @JsonKey(name: "send_timestamp")
  int? sendTimeStamp;

  /// 发送者头像URL
  @JsonKey(name: "sender_avatar")
  String? senderAvatar;

  /// 发送者ID
  @JsonKey(name: "sender_id")
  String? senderId;

  /// 发送者昵称
  @JsonKey(name: "sender_nick")
  String? senderNick;

  @JsonKey(name: "receivers_all", defaultValue: '')
  String? receiversAll;

  @JsonKey(name: "receivers_read", defaultValue: '')
  String? receiversRead;

  /// 默认构造函数
  MessageDb({
    this.convId,
    this.convType,
    this.msgClientId,
    this.msgContent,
    this.msgContentFmt,
    this.msgContentType,
    this.msgId,
    this.msgReadTime,
    this.msgStatus,
    this.msgType,
    this.sendTimeStamp,
    this.senderAvatar,
    this.senderId,
    this.senderNick,
    this.isMyself,
    this.msgStatusTips,
    this.receiversAll,
    this.receiversRead,
  });

  /// 从JSON映射创建消息实体对象
  factory MessageDb.fromJson(Map<String, dynamic> json) => _$MessageDbFromJson(json);

  /// 将消息实体对象转换为JSON映射
  Map<String, dynamic> toJson() => _$MessageDbToJson(this);

  MessageDb fromSocket(Map<String, dynamic> data) {
    MessageDb entity = MessageDb.fromJson(data);
    Map map = data['msg_content'];
    entity.msgClientId = map['msg_client_id'];
    entity.msgId = map['msg_id'];
    entity.isMyself = map['is_myself'];
    entity.msgStatus = map['status'];
    entity.msgStatusTips = map['status_tips'];
    entity.msgContent = map['info'];
    List receiversAll = map['receivers_all'] ?? [];
    if (receiversAll.isNotEmpty) {
      entity.receiversAll = jsonEncode(receiversAll);
    } else {
      entity.receiversAll = '';
    }
    List receiversRead = map['receivers_read'] ?? [];
    if (receiversRead.isNotEmpty) {
      entity.receiversRead = jsonEncode(receiversRead);
    } else {
      entity.receiversRead = '';
    }

    return entity;
  }
}
